package com.ssm.helong.shop.service.impl;

import com.ssm.helong.shop.common.ServiceResultEnum;
import com.ssm.helong.shop.util.BeanUtil;
import com.ssm.helong.shop.Dao.GoodsCategoryMapper;

import com.ssm.helong.shop.controller.vo.ShopsIndexCategoryVO;

import com.ssm.helong.shop.entity.GoodsCategory;
import com.ssm.helong.shop.service.GoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.DateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;


@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    /**
     * 添加商品分类
     *
     * @param goodsCategory
     * @return
     */
    @Override
    public String addCategory(GoodsCategory goodsCategory) {
        GoodsCategory temp = goodsCategoryMapper.selectByLevelAndName(goodsCategory.getCategoryLevel(), goodsCategory.getCategoryName());
        //查询是否存在，已经有本商品分类
        if (Objects.nonNull(temp)) {
            return ServiceResultEnum.SAME_CATEGORY_EXIST.getResult();
        }
        //添加商品
        Date date = new Date();
        DateFormat df = DateFormat.getDateTimeInstance();
        goodsCategory.setCreateTime(df.format(date));
        if (goodsCategoryMapper.insertSelective(goodsCategory) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    /**
     * 修改商品分类内容
     *
     * @param goodsCategory
     * @return
     */

    @Override
    public String updateGoodsCategory(GoodsCategory goodsCategory) {
        //获取主键 查询商品信息
        GoodsCategory temp = goodsCategoryMapper.selectByPrimaryKey(goodsCategory.getCategoryId());
        //判断是否存在
        if (temp == null) {

            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        Date date = new Date();
        DateFormat df = DateFormat.getDateTimeInstance();
        goodsCategory.setUpdateTime(df.format(date));
        if (goodsCategoryMapper.updateByPrimaryKey(goodsCategory) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

//    @Override
//    public List <ShopsIndexCategoryVO> getCategoriesForIndex() {
//        List <ShopsIndexCategoryVO> newBeeMallIndexCategoryVOS = new ArrayList <>();
//        //获取一级分类的固定数量的数据
//        List <GoodsCategory> firstLevelCategories = goodsCategoryMapper.selectByLevelAndParentIdsAndNumber(Collections.singletonList(0), 1, 10);
//        if (!CollectionUtils.isEmpty(firstLevelCategories)) {
//            //遍历父类id    ：.map(GoodsCategory::getCategoryId).collect(Collectors.toList()
//            List <Integer> firstLevelCategoryIds = firstLevelCategories.stream().map(GoodsCategory::getCategoryId).collect(Collectors.toList());
//            //获取二级分类的数据
//            List <GoodsCategory> secondLevelCategories = goodsCategoryMapper.selectByLevelAndParentIdsAndNumber(firstLevelCategoryIds, 2, 0);
//            if (!CollectionUtils.isEmpty(secondLevelCategories)) {
//                List <Integer> secondLevelCategoryIds = secondLevelCategories.stream().map(GoodsCategory::getCategoryId).collect(Collectors.toList());
//                //获取三级分类的数据
//                List <GoodsCategory> thirdLevelCategories = goodsCategoryMapper.selectByLevelAndParentIdsAndNumber(secondLevelCategoryIds, 3, 0);
//                if (!CollectionUtils.isEmpty(thirdLevelCategories)) {
//                    //根据 parentId 将 thirdLevelCategories 分组
//                    Map <Integer, List <GoodsCategory>> thirdLevelCategoryMap = thirdLevelCategories.stream().collect(groupingBy(GoodsCategory::getParentId));
//                    List <SecondLevelCategoryVO> secondLevelCategoryVOS = new ArrayList <>();
//                    //处理二级分类
//                    for (GoodsCategory secondLevelCategory : secondLevelCategories) {
//                        SecondLevelCategoryVO secondLevelCategoryVO = new SecondLevelCategoryVO();
//
//                        //BeanUtils可以帮助我们把属性封装到javabean对象的对应属性中，比如可以把提交的表单数据封装到一个实体对象
//                        //目的是类型自动转换！！！！！！！！！！！！！！主要使用的原因
//                        BeanUtil.copyProperties(secondLevelCategory, secondLevelCategoryVO);
//                        //如果该二级分类下有数据则放入 secondLevelCategoryVOS 对象中
//                        if (thirdLevelCategoryMap.containsKey(secondLevelCategory.getCategoryId())) {
//                            //根据二级分类的id取出thirdLevelCategoryMap分组中的三级分类list
//                            List <GoodsCategory> tempGoodsCategories = thirdLevelCategoryMap.get(secondLevelCategory.getCategoryId());
//                            secondLevelCategoryVO.setThirdLevelCategoryVOS((BeanUtil.copyList(tempGoodsCategories, ThirdLevelCategoryVO.class)));
//                            secondLevelCategoryVOS.add(secondLevelCategoryVO);
//                        }
//                    }
//                    //处理一级分类
//                    if (!CollectionUtils.isEmpty(secondLevelCategoryVOS)) {
//                        //根据 parentId 将 thirdLevelCategories 分组
//                        Map <Integer, List <SecondLevelCategoryVO>> secondLevelCategoryVOMap = secondLevelCategoryVOS.stream().collect(groupingBy(SecondLevelCategoryVO::getParentId));
//                        for (GoodsCategory firstCategory : firstLevelCategories) {
//                            ShopsIndexCategoryVO newBeeMallIndexCategoryVO = new ShopsIndexCategoryVO();
//                            BeanUtil.copyProperties(firstCategory, newBeeMallIndexCategoryVO);
//                            //如果该一级分类下有数据则放入 newBeeMallIndexCategoryVOS 对象中
//                            if (secondLevelCategoryVOMap.containsKey(firstCategory.getCategoryId())) {
//                                //根据一级分类的id取出secondLevelCategoryVOMap分组中的二级级分类list
//                                List <SecondLevelCategoryVO> tempGoodsCategories = secondLevelCategoryVOMap.get(firstCategory.getCategoryId());
//                                newBeeMallIndexCategoryVO.setSecondLevelCategoryVOS(tempGoodsCategories);
//                                newBeeMallIndexCategoryVOS.add(newBeeMallIndexCategoryVO);
//                            }
//                        }
//                    }
//                }
//            }
//            return newBeeMallIndexCategoryVOS;
//        } else {
//            return null;
//        }
//    }

    @Override
    public List <GoodsCategory> findAllGoodsCategory() {
        //查询所有商品列表
        List <GoodsCategory> list = goodsCategoryMapper.selectFindAllGoods();
        return buildRoot(list, 0);
    }

    public static <T extends GoodsCategory> List <T> buildRoot(List <T> treeNodes, Integer root) {
        List <T> nodes = new ArrayList <>();
        //遍历商品的List
        for (T treeNode : treeNodes) {
            //判断条件 如果对象的父类id等于0 就意味这它为第一层的商品。
            if (treeNode.getParentId().equals(root)) {
                //给下级对象附上等级
                treeNode.setLevel(1);
                //调用buildChildren
                nodes.add(buildChildren(treeNode, treeNodes));
            }
        }
        return nodes;
    }

    public static <T extends GoodsCategory> T buildChildren(T nowNode, List <T> treeNodes) {
        //遍历 treeNodes对象
        for (T treeNode : treeNodes) {
            //判断 nowNode的商品id是否等于treeNode的父类Id
            if (nowNode.getCategoryId().equals(treeNode.getParentId())) {
                //如果没用对象的id为其他对象的父类id，则循环结束 否则进行buildChildren的递归
                if (CollectionUtils.isEmpty(nowNode.getChildGoodsCategory())) {
                    nowNode.setChildGoodsCategory(new ArrayList <>());
                }
                treeNode.setLevel(nowNode.getLevel() + 1);
                nowNode.addChild(buildChildren(treeNode, treeNodes));

            }
        }
        return nowNode;
    }

}
